package com.jsy.imagepicker;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.io.File;
import java.util.ArrayList;

public abstract class PickerAdapter extends RecyclerView.Adapter<PickerAdapter.Holder> {

    Activity mActivity;
    ImagePicker.Parameters mParameters;
    int mItemHeight;
    public static ArrayList<String> checkedList = null;
    public static ArrayList<String> allList = null;


    public PickerAdapter(Activity activity, ImagePicker.Parameters parameters, int itemHeight) {
        mActivity = activity;
        mParameters = parameters;
        mItemHeight = itemHeight;
        checkedList = new ArrayList<>();
        allList = new ArrayList<>();
        allList.addAll(PickerUtils.getImages(mActivity, parameters.mFilterGif));

    }

    /**
     * 相册图片变动后刷新
     */
    public void refreshData() {
        allList.clear();
        allList.addAll(PickerUtils.getImages(mActivity, mParameters.mFilterGif));
        notifyDataSetChanged();
    }


    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_image_photo_picker, parent, false);
        Holder holder = new Holder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(Holder holder, final int position) {
        final String item;
        if (mParameters.mUseCamera) {
            if (position == 0) {
                item = "camera";
            } else {
                item = allList.get(position - 1);
            }
        } else {
            item = allList.get(position);
        }
        if ("camera".equals(item)) {
            holder.llCheck.setVisibility(View.GONE);
            holder.tvType.setVisibility(View.GONE);
            holder.iv.setImageResource(R.drawable.camera);
            holder.iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    openCamera();
                }
            });
        } else {
            if (checkedList.contains(item)) {
                if (mParameters.mMaxCount == 1){
                    holder.tvCheck.setBackgroundResource(R.drawable.radio_checked);
                    holder.tvCheck.setText("");
                }else {
                    holder.tvCheck.setBackgroundResource(R.drawable.bg_green_circle);
                    holder.tvCheck.setText((checkedList.indexOf(item)+1)+"");
                }
            } else {
                holder.tvCheck.setBackgroundResource(R.drawable.radio_unchecked);
                holder.tvCheck.setText("");
            }
            holder.llCheck.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!checkedList.contains(item)) {
                        if (mParameters.mMaxCount == 1) {
                            checkedList.clear();
                            checkedList.add(item);
                            notifyDataSetChanged();
                            onImageChecked(mParameters.mMaxCount, checkedList.size());
                        } else {
                            if (checkedList.size() < mParameters.mMaxCount) {
                                checkedList.add(item);
                                notifyDataSetChanged();
                                onImageChecked(mParameters.mMaxCount, checkedList.size());
                            }
                        }
                    } else {
                            checkedList.remove(item);
                            notifyDataSetChanged();
                            onImageChecked(mParameters.mMaxCount, checkedList.size());
                    }

                }
            });
            holder.iv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mActivity.startActivityForResult(
                            new Intent(mActivity, ImagePreviewActivity.class)
                                    .putExtra("position", mParameters.mUseCamera ? position - 1 : position)
                                    .putExtra("count", mParameters.mMaxCount), ImagePickerActivity.ACTION_TO_PREVIEW
                    );
                }
            });
            Glide.with(mActivity).load(item).apply(new RequestOptions().dontAnimate()).into(holder.iv);
            if (PickerUtils.isGifFile(new File(item))) {
                holder.tvType.setVisibility(View.VISIBLE);
                holder.tvType.setText("gif");
            } else {
                holder.tvType.setVisibility(View.GONE);
            }
        }

    }


    @Override
    public int getItemCount() {
        if (mParameters.mUseCamera) {
            return allList.size() + 1;
        } else {
            return allList.size();
        }

    }


    /**
     * 选择项变动时
     *
     * @param count
     * @param checkedCount
     */
    public abstract void onImageChecked(int count, int checkedCount);

    /**
     * 启动摄像头拍照
     */
    public abstract void openCamera();


    class Holder extends RecyclerView.ViewHolder {

        ImageView iv;
        LinearLayout llCheck;
        TextView tvCheck;
        TextView tvType;

        public Holder(View itemView) {
            super(itemView);
            iv = itemView.findViewById(R.id.iv);
            llCheck = itemView.findViewById(R.id.ll_check);
            tvCheck = itemView.findViewById(R.id.tv_check);
            ViewGroup.LayoutParams lp = iv.getLayoutParams();
            lp.height = mItemHeight;
            iv.setLayoutParams(lp);
            tvType = itemView.findViewById(R.id.tv_type);
        }
    }
}
